<?php

namespace app\admin\service;

use app\model\Videos;
use app\model\VideoTemplate;
// use think\facade\Session;
use think\facade\Cache;
use think\facade\Db;

class VideoTemplateService
{
    private $model;
    private $videotemplateModel;

    public function __construct(Videos $model, VideoTemplate $videotemplateModel)
    {
        $this->model = $model;
        $this->videotemplateModel = $videotemplateModel;
    }
    //查询分组数据
    public function getList($params){
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;
        $where[] = ['status', '<>', 2];
        if (!empty($params['name'])) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        if (!empty($params['video_id'])) {
            $where[] = ['video_ids', 'like', '%' . $params['video_id'] . '%'];
        }
        $data = $this->videotemplateModel->getPagingDataByWhere($where,$page,$limit,'*');
        $video_name=[];
        if(!empty($data['data'])) {
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['video_name'] ='';
                $video_name = $this->model->getDataByIds($value['video_arr'], 'name');
                if($video_name) {
                    $name=array_column($video_name,'name');
                    $data['data'][$key]['video_name'] = implode(',', $name);
                }
            }
        }
        return $data;
    }
    //添加修改
    public function editVideoTemplate($params,$userInfo){
        if (empty($params['id'])) {
            $params['uid'] = $userInfo['user_id'];
            $params['created_at'] = time();
        } else {
            $params['updated_at'] = time();
        }
        $id = $this->videotemplateModel->edit($params);
        if (!$id) {
            return false;
        }
        return true;
    }
    public function deleteVideoTemplate($params){
        if (empty($params['id'])) {
            return false;
        }
        $params['updated_at'] = time();
        $params['status']=2;
     
        $id = $this->videotemplateModel->edit($params);
        if (!$id) {
            return false;
        }
        return true;
    }
    public function getVideoDetail($params){
        if (empty($params['id'])) {
            return false;
        }
        $data=$this->videotemplateModel->getInfoById($params['id'],'id,name,video_ids');
        return $data;
    }
    public function getVideoTemplateSelectList($params){
        $where = [
            ['status', '=',1]
        ];
        if (!empty($params['name'])) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        $data = $this->videotemplateModel->getDataByWhere($where, 'id, name');

        return $data;
    }
}
